//
//  HCTabBar.swift
//  Hweibome
//
//  Created by 何川 on 15/12/5.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCTabBar: UITabBar {

    
    var composeButtonClosure: (()->())?
    
    override init (frame:CGRect)
    {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setupUI(){
        backgroundImage = UIImage(named: "tabbar_background")
        addSubview(composeButton)
    }
    
    lazy var composeButton:UIButton =
    {
        let button = UIButton()
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        return button
        
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center =  CGPoint (x: frame.width*0.5, y: frame.height*0.5 )
        let childW = frame.width / 5
        var index = 0
        for childView in subviews
        {
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                let x = CGFloat(index) * childW
                childView.frame = CGRect (x: x, y: 0, width: childW, height: frame.height)
                index++
                if index == 2
                {
                    index++
                }
            }
        }
    }
    
    
 @objc private   func composeButtonClick()
    {
        printLog("撰写按你点击")
        composeButtonClosure?()
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(animated)
//        
//        tabBar.bringSubviewToFront(composedButton)
//    }
}


